unit talonarioOC;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, RXDBCtrl, StdCtrls, Mask, ToolEdit, Provider,
  SqlExpr, DB, DBClient, DBLocal, DBLocalS, DateUtils, Buttons, Menus;

type
  TfrmTalonarioOC = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    grdSueldos: TRxDBGrid;
    s: TDataSource;
    cds: TSQLClientDataSet;
    cdsNUMERO: TIntegerField;
    cdsRAZON: TStringField;
    cdsDENOMINACION: TStringField;
    btnExcel: TSpeedButton;
    cdsESTADO: TSmallintField;
    cdsLogica: TSQLClientDataSet;
    cdsLogicaID: TSmallintField;
    cdsLogicaVALOR: TStringField;
    cdsvalorLogico: TStringField;
    btnNueva: TSpeedButton;
    cdsobs: TStringField;
    cdsOBSERVACIONES: TMemoField;
    edtFiltro: TEdit;
    Label5: TLabel;
    btnDesfiltrar: TBitBtn;
    SpeedButton1: TSpeedButton;
    pop: TPopupMenu;
    CompararOrdendeCompraconFacturas1: TMenuItem;
    AbrirOrdendeCompra1: TMenuItem;
    SpeedButton2: TSpeedButton;
    cdsID_OBRA: TSmallintField;
    SpeedButton3: TSpeedButton;
    cdsID_PROVEEDOR: TSmallintField;
    DuplicarOrdendeCompra1: TMenuItem;
    SpeedButton4: TSpeedButton;
    cdsFECHA: TSQLTimeStampField;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure grdSueldosTitleClick(Column: TColumn);
    procedure grdSueldosDblClick(Sender: TObject);
    procedure edtFechaIniChange(Sender: TObject);
    procedure btnExcelClick(Sender: TObject);
    procedure cdsESTADOChange(Sender: TField);
    procedure btnNuevaClick(Sender: TObject);
    procedure cdsCalcFields(DataSet: TDataSet);
    procedure edtFiltroChange(Sender: TObject);
    procedure edtFiltroKeyPress(Sender: TObject; var Key: Char);
    procedure grdSueldosCellClick(Column: TColumn);
    procedure btnDesfiltrarClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton2MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    Fusuario: word;
    FcolumnaSeleccionada: word;
    procedure cargarGrilla;
    procedure Setusuario(const Value: word);
    procedure SetcolumnaSeleccionada(const Value: word);
  published
    property usuario: word read Fusuario write Setusuario;
    property columnaSeleccionada: word read FcolumnaSeleccionada write SetcolumnaSeleccionada;
  public
    constructor CreateNuevo(AOwner: TComponent; usuario: word);
  end;

var
  frmTalonarioOC: TfrmTalonarioOC;

implementation

uses MDatos, ordenCompra, Recursos, OC_Fact, materialOCDet;

{$R *.dfm}

procedure TfrmTalonarioOC.cargarGrilla;
begin
  self.cds.Close;
  self.cds.Params[0].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
  self.cds.Open;
end;

constructor TfrmTalonarioOC.CreateNuevo(AOwner: TComponent; usuario: word);
begin
  inherited Create(Aowner);
  self.usuario := usuario;
  self.edtFechaIni.Date := IncMonth(now, -1);
  self.edtFechaFin.Date := now;
  self.cargarGrilla;
  self.ShowModal;
end;

procedure TfrmTalonarioOC.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.cds.Close;
  Action := caFree;
end;

procedure TfrmTalonarioOC.grdSueldosTitleClick(Column: TColumn);
begin
  if (Column.FieldName <> 'valorLogico') then
    self.cds.IndexFieldNames := Column.FieldName+'; FECHA';
end;

procedure TfrmTalonarioOC.grdSueldosDblClick(Sender: TObject);
begin
  TfrmOrdenCompra.Crear(self, self.cdsNUMERO.AsInteger, self.usuario, 0, 0, 0);
  self.cds.Refresh;
end;

procedure TfrmTalonarioOC.Setusuario(const Value: word);
begin
  Fusuario := Value;
end;

procedure TfrmTalonarioOC.edtFechaIniChange(Sender: TObject);
begin
  self.cargarGrilla;
end;

procedure TfrmTalonarioOC.btnExcelClick(Sender: TObject);
begin
  abrirEnExcel(self, self.cds, nil, nil, 'LISTADO DE ORDENES DE COMPRA', 'INTERVALO DEFINIDO: del '
  +DateTimeToStr(self.edtFechaIni.Date)+' al '+DateTimeToStr(self.edtFechaFin.Date));
end;

procedure TfrmTalonarioOC.cdsESTADOChange(Sender: TField);
var
  ds: TSQLDataSet;
begin
  ds := TSQLDataSet.Create(self);
  try
    ds.SQLConnection := Datos.Con;
    ds.CommandText := 'UPDATE ordencompra SET estado = '+Sender.AsString
    +' WHERE numero = '+self.cdsNUMERO.AsString;
    ds.ExecSQL();
  finally
    ds.Free;
  end;
  cds.Close;
  cds.Open;
end;

procedure TfrmTalonarioOC.btnNuevaClick(Sender: TObject);
begin
  TfrmOrdenCompra.Crear(self, 0, self.usuario, 0, 0, 0);
  self.cds.Refresh;
end;

procedure TfrmTalonarioOC.cdsCalcFields(DataSet: TDataSet);
begin
  self.cdsobs.AsString := self.cdsOBSERVACIONES.AsString;
end;

procedure TfrmTalonarioOC.edtFiltroChange(Sender: TObject);
begin
  self.btnDesfiltrar.Enabled := self.edtFiltro.Text <> '';
end;

procedure TfrmTalonarioOC.edtFiltroKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = chr(VK_RETURN) then
  begin
    self.cds.Filtered := False;
    if (self.edtFiltro.Text <> '') then
    begin
      if self.columnaSeleccionada = 5 then
        self.cds.Filter := 'OBSERVACIONES LIKE ''%'+self.edtFiltro.Text+'%'''
      else
        self.cds.Filter := self.grdSueldos.Columns [self.columnaSeleccionada].FieldName+' LIKE ''%'+self.edtFiltro.Text+'%''';
      self.cds.Filtered := True;
    end;
  end;
end;

procedure TfrmTalonarioOC.SetcolumnaSeleccionada(const Value: word);
begin
  FcolumnaSeleccionada := Value;
end;

procedure TfrmTalonarioOC.grdSueldosCellClick(Column: TColumn);
var
  i : word;
begin
  if (Column.Title.Font.Color <> clRed) and (Column.Field.DataType = ftString)then
  begin
    for i := 0 to self.grdSueldos.Columns.Count-1 do
      self.grdSueldos.Columns [i].Title.Font.Color := Column.Title.Font.Color;
    Column.Title.Font.Color := clRed;
    self.columnaSeleccionada := self.grdSueldos.Col-1;
  end;
end;

procedure TfrmTalonarioOC.btnDesfiltrarClick(Sender: TObject);
var
  Key: char;
begin
  Key := chr(VK_RETURN);
  self.edtFiltro.Text := '';
  self.edtFiltro.SetFocus;
  self.edtFiltroKeyPress(Sender, Key);
end;

procedure TfrmTalonarioOC.SpeedButton1Click(Sender: TObject);
begin
  TfrmOC_Fact.CreateNuevo(self, self.usuario, self.cdsNUMERO.AsInteger);
end;

procedure TfrmTalonarioOC.SpeedButton2Click(Sender: TObject);
begin
  //TfrmMaterialOCDet.CreateNuevo(self, self.cdsID_OBRA.AsString, incYear(now, -1), now, True);
  TfrmMaterialOCDet.CreateNuevo(self, self.cdsID_OBRA.AsString, self.edtFechaIni.Date, self.edtFechaFin.Date, False);
end;

procedure TfrmTalonarioOC.SpeedButton2MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  self.SpeedButton2.Hint := 'Detallar materiales solicitados de Obra: '+self.cdsDENOMINACION.AsString;
end;

procedure TfrmTalonarioOC.SpeedButton3Click(Sender: TObject);
begin
    TfrmOrdenCompra.Crear(self, 0, self.usuario, 0, self.cdsID_PROVEEDOR.AsInteger, self.cdsNUMERO.AsInteger);
end;

procedure TfrmTalonarioOC.SpeedButton4Click(Sender: TObject);
begin
  self.cds.IndexFieldNames := '';
end;

end.
